package javasec.contempt.visuals;

import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.swt.SWT;
import org.eclipse.swt.awt.SWT_AWT;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.swt.widgets.TableItem;
import org.eclipse.swt.widgets.Text;
import org.eclipse.ui.IEditorInput;
import org.eclipse.ui.IEditorSite;
import org.eclipse.ui.PartInitException;
import org.eclipse.ui.part.EditorPart;


import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Properties;
import java.util.Vector;

import javasec.contempt.data.SeedConnection;
import javasec.contempt.data.SeedConnector;

import javax.sql.DataSource;

public class AssetsVisual extends EditorPart
{
	public static String ID = "javasec.contempt.visuals.AssetVisual";
	//private Text entry;
	String tableName;
	private SeedConnection seedConnection=null;
	private Table table=null;
	private Display display=null;
	
	public AssetsVisual()
	{
		// TODO Auto-generated constructor stub
	}

	public void doSave(IProgressMonitor monitor)
	{
		// TODO Auto-generated method stub

	}

	public void doSaveAs()
	{
		// TODO Auto-generated method stub

	}

	public void init(IEditorSite site, IEditorInput input)
			throws PartInitException
	{
		System.out.println("new database editor init()ed");
		// TODO Auto-generated method stub
		setSite(site);
		setInput(input);
		
		System.out.println("get the database name!");
		tableName=input.getName();
		seedConnection=((VisualInput)input).seedConnection;
		setPartName(tableName);
	}

	public boolean isDirty()
	{
		// TODO Auto-generated method stub
		return false;
	}

	public boolean isSaveAsAllowed()
	{
		// TODO Auto-generated method stub
		return false;
	}
	
	public void createPartControl(Composite parent)
	{		
		// save display for async exec later
		display=Display.getCurrent();
		
		// create the table
		table = new Table(parent,SWT.VIRTUAL | SWT.BORDER);
		int numColumns=0;
		String[] fieldNames=null;
		try
		{
			fieldNames=seedConnection.dataSeed.getFields(tableName);
		}
		catch(Exception e)
		{
			e.printStackTrace();
		}
		
		for(String fieldName:fieldNames)
		{
			TableColumn col=new TableColumn(table,SWT.LEFT);
			col.setText(fieldName);
			col.pack();
			++numColumns;
		}
			
		TableColumn spacer=new TableColumn(table,SWT.CENTER);
		spacer.setText("");
		spacer.pack();
			
		table.setHeaderVisible(true);
			
		Object[][] rows=null;
		try
		{
			rows=seedConnection.dataSeed.selectAll(tableName);	
		}
		catch(Exception e)
		{
			e.printStackTrace();
		}
		
		System.out.println("num rows found: "+rows.length);
		for(Object[] row:rows)
		{
			TableItem item= new TableItem(table,SWT.NONE);
			String colData[] = new String[numColumns];
			
			for(int x=0;x<numColumns;++x)
			{
				if(row[x]==null)colData[x]="null";
				else colData[x]=row[x].toString();
			}
			
			item.setText(colData);
		}

		// repack all the columns (make sure they are the right size)
		for(int x=0;x<numColumns;++x)
		{
			table.getColumn(x).pack();
		}
		
		seedConnection.notifier.subscribe("data.newRow."+tableName,this,"addRow");
	}

	public void addRow(Object o)
	{
		try
		{
			final Object[] row=(Object[])o;
			final String[] rowStrings=new String[row.length];
		
			rowStrings[0]=""; // XXX just add "" for now, later maybe add row number
			for(int x=0;x<row.length;++x)
			{
				rowStrings[x]=row[x].toString();
			}
		
			// add new table item (must be in display thread to do so)
			display.asyncExec(new Runnable()
			{
				public void run()
				{
					TableItem newItem=new TableItem(table,SWT.NONE);
					newItem.setText(rowStrings);
					
					// repack all the columns (make sure they are the right size)
					for(int x=0;x<table.getColumnCount();++x)
					{
						table.getColumn(x).pack();
					}		
				}
			});		
		}
		catch(Exception e)
		{
			e.printStackTrace();
		}
	}
	
	public void setFocus()
	{
	}
}
